API Gateway হল একটি সার্ভিস যা বিভিন্ন মাইক্রোসার্ভিসে একক প্রবেশপথ হিসেবে কাজ করে এবং ক্লায়েন্টের জন্য একাধিক সার্ভিসে রিকোয়েস্ট পাঠানোর কাজে সহায়ক হয়। এটি সাধারণত রাউটিং, লোড ব্যালান্সিং, অথেনটিকেশন, রেট লিমিটিং, এবং অন্যান্য ফিচার পরিচালনা করতে ব্যবহৃত হয়।
Spring Boot অ্যাপ্লিকেশনের মধ্যে API Gateway Integration করার জন্য Spring Cloud Gateway একটি জনপ্রিয় টুল, যা API Gateway হিসেবে কাজ করে।
এখানে Spring Boot Client এর মধ্যে API Gateway Integration এর উদাহরণ দেখানো হলো।
প্রয়োজনীয় ডিপেনডেন্সি
১. Spring Cloud Gateway এর জন্য Maven ডিপেনডেন্সি:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
২. Spring Boot WebFlux (রিঅ্যাকটিভ রিস্পন্স হ্যান্ডলিং জন্য):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
৩. Spring Cloud Config (যদি সেন্ট্রাল কনফিগারেশন দরকার হয়):
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Spring Cloud Gateway Configuration
Spring Cloud Gateway API Gateway হিসেবে কাজ করার জন্য application.yml কনফিগারেশন ফাইল ব্যবহার করা হয়। এই কনফিগারেশনে বিভিন্ন রুট এবং ফিল্টার সেট করা যায়।
1. application.yml কনফিগারেশন:
spring:
cloud:
gateway:
routes:
- id: api-service
uri: http://localhost:8081 # Target service URI
predicates:
- Path=/api/** # Matching API requests
filters:
- AddRequestHeader=X-Request-Foo, Bar # Request header filter
এখানে http://localhost:8081 সার্ভিসে যেকোনো /api/** পাথে রিকোয়েস্ট রুট করা হচ্ছে এবং "X-Request-Foo: Bar" হেডার যুক্ত করা হচ্ছে।
API Gateway এর সাথে Service Integration
ধরা যাক, আমাদের দুইটি মাইক্রোসার্ভিস রয়েছে:
- Service A (এটি API Gateway এর মাধ্যমে ক্লায়েন্ট রিকোয়েস্ট গ্রহণ করবে)
- Service B (এটি API Gateway এর মাধ্যমে রাউট হবে)
এখন API Gateway এর মাধ্যমে রিকোয়েস্ট পাঠানোর জন্য Service A এবং Service B দুইটি স্প্রিং বুট অ্যাপ্লিকেশন তৈরি করা হবে।
Service A (Spring Boot API)
Service A একটি সহজ Spring Boot অ্যাপ্লিকেশন যা /api/data এ রেসপন্স দেয়।
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ServiceAController {
@GetMapping("/api/data")
public String getData() {
return "Data from Service A";
}
}
Service A এই রেসপন্সটি /api/data পাথের জন্য প্রদান করবে।
Service B (Spring Boot API)
Service B অন্য একটি Spring Boot অ্যাপ্লিকেশন যা Service A এর রাউটের মাধ্যমে কল করবে।
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ServiceBController {
private final RestTemplate restTemplate = new RestTemplate();
@GetMapping("/api/data-from-a")
public String getDataFromA() {
String url = "http://localhost:8080/api/data"; // API Gateway route
return restTemplate.getForObject(url, String.class);
}
}
Service B-এর /api/data-from-a রাউটটি Service A থেকে ডেটা নিয়ে আসবে API Gateway এর মাধ্যমে।
Spring Boot Client (Client Application)
এখন ক্লায়েন্ট অ্যাপ্লিকেশন তৈরি করা হবে যা API Gateway এর মাধ্যমে Service A এর /api/data রাউটটিতে কল করবে।
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ClientController {
private final RestTemplate restTemplate = new RestTemplate();
@GetMapping("/get-data")
public String getData() {
// API Gateway থেকে Service A এর ডেটা ফেচ করা
String apiGatewayUrl = "http://localhost:8080/api/data"; // API Gateway URL
return restTemplate.getForObject(apiGatewayUrl, String.class);
}
}
এখানে http://localhost:8080/api/data URL ক্লায়েন্টের মাধ্যমে API Gateway থেকে কল হচ্ছে। API Gateway, সার্ভিস A তে রিকোয়েস্ট পাঠাবে এবং সেখান থেকে রেসপন্স ফিরে আসবে।
API Gateway রাউটিং বিশদ:
এখানে API Gateway এর routes কনফিগারেশন করা হচ্ছে, যাতে নির্দিষ্ট পাথ /api/** এ ক্লায়েন্ট রিকোয়েস্ট এসে একটি নির্দিষ্ট সার্ভিসে পৌঁছাতে পারে।
URI and Filters:
uri: http://localhost:8081: Target সার্ভিসের URL যেখানে API Gateway রিকোয়েস্ট রাউট করবে।predicates: এগুলো হল শর্ত যা যাচাই করে রিকোয়েস্টটি কোথায় রাউট হবে।filters: অতিরিক্ত হেডার বা মডিফিকেশন যা রিকোয়েস্টে করা হয়।
API Gateway Configuration কনফিগারেশন (যদি Service Discovery ব্যবহার করা হয়):
spring:
cloud:
gateway:
routes:
- id: service-a
uri: lb://SERVICE-A # Service Discovery via Eureka
predicates:
- Path=/api/** # Matching API requests
এখানে Service Discovery ব্যবহার করা হচ্ছে। যদি সার্ভিসগুলি Eureka বা অন্য কোনো সার্ভিস ডিসকভারি সিস্টেমে রেজিস্টার থাকে, তবে API Gateway সেই সার্ভিসগুলোর দিকে রিকোয়েস্ট পাঠাবে।
প্রদর্শন (Example Execution)
- Service A:
/api/dataপাথের মাধ্যমে ডেটা সরবরাহ করবে। - Service B:
/api/data-from-aপাথের মাধ্যমে Service A থেকে ডেটা নেবে। - Client:
/get-dataপাথের মাধ্যমে API Gateway এর মাধ্যমে Service A থেকে ডেটা ফেচ করবে।
Client Request Example:
GET http://localhost:8082/get-data
এটি API Gateway এর মাধ্যমে Service A তে কল পাঠাবে এবং Service A থেকে ডেটা ফিরিয়ে আনবে।
Conclusion
Spring Boot Client এর মাধ্যমে API Gateway Integration ব্যবহার করে আপনি একাধিক মাইক্রোসার্ভিসের মধ্যে রিকোয়েস্ট রাউটিং, অথেনটিকেশন, ফিল্টারিং, এবং লোড ব্যালান্সিং পরিচালনা করতে পারেন। Spring Cloud Gateway এর মাধ্যমে এই ফিচারগুলো সহজেই ইমপ্লিমেন্ট করা সম্ভব।
Read more